package com.nexgo.payment.trans.setting.password;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.nexgo.payment.R;
import com.nexgo.payment.constrant.PreferencesConstants;
import com.nexgo.payment.databases.PasswordInfo;
import com.nexgo.payment.databases.Repository;
import com.nexgo.payment.fragment.EditDialogFragment;
import com.nexgo.payment.trans.setting.SaveParameterActivity;
import com.nexgo.payment.trans.setting.SaveParameterPresenter;
import com.nexgo.payment.util.StringUtils;
import com.nexgo.payment.util.ToastUtils;
import com.nexgo.payment.util.UIUtils;
import com.nexgo.payment.widget.TitleBuilder;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 密码管理设置
 *
 * @author 谭忠扬-YuriTam
 * @time 2017年7月6日
 */
public class SetupPasswordActivity extends SaveParameterActivity {
    private static final String TAG = SetupPasswordActivity.class.getSimpleName();

    @BindView(R.id.setup_manage_password)
    EditText setupManagePassword;
    @BindView(R.id.setup_charge_password)
    EditText setupChargePassword;
    @BindView(R.id.setup_security_password)
    EditText setupSecurityPassword;

    private EditDialogFragment mDialogFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup_password);
        ButterKnife.bind(this);
        new SaveParameterPresenter(this, Repository.getInstance());

        initTitle();
        initEvent();
    }

    //初始化标题栏
    private void initTitle() {
        new TitleBuilder(this)
                .setExternalTitleBgRes(R.drawable.bg_blue_icon)
                .setLeftImage(R.drawable.arrow_icon)
                .setTitleText(getString(R.string.password_manage_title))
                .build();
    }

    /**
     * 初始化事件
     */
    private void initEvent(){
        //取消输入法
        setupManagePassword.setKeyListener(null);
        setupChargePassword.setKeyListener(null);
        setupSecurityPassword.setKeyListener(null);
    }

    @Override
    protected HashMap<String, String> getDataBind() {
        HashMap<String, String> map = new HashMap<>();
        return map;
    }

    @Override
    public void showDetail() {
        PasswordInfo info = mPresenter.getPasswordInfo();
        Log.d("Password", info.toString());
        if (info != null){
            setupManagePassword.setText((TextUtils.isEmpty(info.getManagePassword()) ? "" : StringUtils.leftPad("*", 8, '*')));
            setupChargePassword.setText((TextUtils.isEmpty(info.getAdminPassword()) ? "" : StringUtils.leftPad("*", 6, '*')));
            setupSecurityPassword.setText((TextUtils.isEmpty(info.getSecurityPassword()) ? "" : StringUtils.leftPad("*", 8, '*')));
        }
    }

    @OnClick({R.id.title_iv_left, R.id.setup_manage_password,
            R.id.setup_charge_password, R.id.setup_security_password})
    public void onClicked(View view) {
        if (UIUtils.isDoubleClick()){
            return;
        }
        switch (view.getId()) {
            case R.id.title_iv_left:
                finish();
                break;
            case R.id.setup_manage_password:
                showEditDialogFragment(PreferencesConstants.MANAGE_PASSWORD);
                break;
            case R.id.setup_charge_password:
                showEditDialogFragment(PreferencesConstants.ADMIN_PASSWORD);
                break;
            case R.id.setup_security_password:
                showEditDialogFragment(PreferencesConstants.SECURITY_PASSWORD);
                break;
            default:
                break;
        }
    }

    /**
     * 编辑密码
     *
     * @param key 密码类型
     */
    private void showEditDialogFragment(String key){
        if (mDialogFragment == null){
            mDialogFragment = new EditDialogFragment();
        }
        mDialogFragment.setDialogType(EditDialogFragment.DIALOG_TYPE_WARNING);
        switch (key){
            case PreferencesConstants.MANAGE_PASSWORD:
                mDialogFragment.addTitle(getString(R.string.edit_manage_password));
                mDialogFragment.addEditText1(InputType.TYPE_CLASS_NUMBER, 8, getString(R.string.plz_input_org_manage_password), true);
                mDialogFragment.addEditText2(InputType.TYPE_CLASS_NUMBER, 8, getString(R.string.plz_input_new_manage_password), true);
                break;
            case PreferencesConstants.ADMIN_PASSWORD:
                mDialogFragment.addTitle(getString(R.string.edit_admin_password));
                mDialogFragment.addEditText1(InputType.TYPE_CLASS_NUMBER, 6, getString(R.string.plz_input_org_admin_password), true);
                mDialogFragment.addEditText2(InputType.TYPE_CLASS_NUMBER, 6, getString(R.string.plz_input_new_admin_password), true);
                break;
            case PreferencesConstants.SECURITY_PASSWORD:
                mDialogFragment.addTitle(getString(R.string.edit_security_password));
                mDialogFragment.addEditText1(InputType.TYPE_CLASS_NUMBER, 8, getString(R.string.plz_input_org_security_password), true);
                mDialogFragment.addEditText2(InputType.TYPE_CLASS_NUMBER, 8, getString(R.string.plz_input_new_security_password), true);
                break;
            default:
                break;
        }
        mDialogFragment.setPositionText(getString(R.string.action_save));
        mDialogFragment.setNegativeText(getString(R.string.cancel));
        mDialogFragment.setDialogListener(new EditDialogFragment.OnDialogListener() {
            @Override
            public void onCancel() {}

            @Override
            public void onConfirm(List<String> values) {
                mPresenter.savePassword(key, values);
            }
        });
        mDialogFragment.show(getSupportFragmentManager(), null);
    }

    @Override
    public void showSaveParamSuccessfully() {
        if (mDialogFragment != null && mDialogFragment.getDialog() != null){
            Dialog dialog = mDialogFragment.getDialog();
            if (dialog.isShowing()) mDialogFragment.dismiss();
        }
        ToastUtils.showToast(getString(R.string.password_update_success));
    }

    public static Intent newInstance(Context context) {
        return new Intent(context, SetupPasswordActivity.class);
    }

}
